<template>
	<div class="layout-padding">
		<splitpanes>
			<pane size="20">
				<div class="layout-padding-auto layout-padding-view">
					<el-input prefix-icon="search" v-model="filterText" placeholder="请输入关键字搜索" class="mb-2" />
					<el-scrollbar>
						<el-tree
							ref="treeRef"
							:data="treeData.list"
							:props="treeData.defaultProps"
							@node-click="handleNodeClick"
							node-key="id"
							:default-expand-all="true"
							:highlight-current="true"
							:expand-on-click-node="false"
							:filter-node-method="filterNode"
						/>
					</el-scrollbar>
				</div>
			</pane>
			<pane class="ml8">
				<div class="layout-padding-auto layout-padding-view">
					<el-row v-show="showSearch">
						<el-form :model="state.queryForm" size="small" ref="queryRef" :inline="true" @keyup.enter="getDataList">
							<el-form-item label="设备状态" prop="status">
								<el-select v-model="state.queryForm.status" placeholder="请选择设备状态">
									<el-option :value="2" label="在线"></el-option>
									<el-option :value="1" label="离线"></el-option>
								</el-select>
							</el-form-item>
							<el-form-item label="所属区域" prop="airSiteNameLike">
								<el-input placeholder="请输入所属区域" v-model="state.queryForm.airSiteNameLike" />
							</el-form-item>
							<el-form-item label="设备编码" prop="equipmentCodeLike">
								<el-input placeholder="请输入设备编码" v-model="state.queryForm.equipmentCodeLike" />
							</el-form-item>
							<el-form-item label="设备名称" prop="equipmentNameLike">
								<el-input placeholder="请输入设备名称" v-model="state.queryForm.equipmentNameLike" />
							</el-form-item>
							<el-form-item>
								<el-button icon="search" type="primary" @click="handleSearch()"> 查询 </el-button>
								<el-button icon="Refresh" @click="resetQuery">重置</el-button>
							</el-form-item>
						</el-form>
					</el-row>
					<el-row>
						<div class="flex items-center justify-between mb8" style="width: 100%">
							<div class="text-primary">在线设备：{{ statistic?.online || 0 }} / {{ statistic?.total || 0 }}</div>
							<right-toolbar v-model:showSearch="showSearch" class="ml10 mr20" style="float: right" @queryTable="getDataList"></right-toolbar>
						</div>
					</el-row>
					<el-table
						:data="state.dataList"
						v-loading="state.loading"
						border
						:cell-style="tableStyle.cellStyle"
						:header-cell-style="tableStyle.headerCellStyle"
					>
						<el-table-column prop="equipmentCode" label="设备编码" show-overflow-tooltip />
						<el-table-column prop="name" label="设备名称" show-overflow-tooltip />
						<el-table-column prop="address" label="安装地点" show-overflow-tooltip min-width="200" />
						<el-table-column prop="airSiteName" label="所属区域" show-overflow-tooltip />
						<el-table-column prop="recordTime" label="采集时间" show-overflow-tooltip width="160" />
						<el-table-column prop="status" label="设备状态" width="90">
							<template #default="{ row }">
								<el-tag :type="row.status === 2 ? 'success' : 'danger'">{{ row.status === 2 ? '在线' : '离线' }}</el-tag>
							</template>
						</el-table-column>
						<el-table-column label="操作" width="110" fixed="right">
							<template #default="scope">
								<el-tooltip content="实时数据">
									<el-button text type="primary" v-auth="'platform_airMonitorEquipment_edit'" @click="dialogRef.openDialog(scope.row)"
										>实时数据</el-button
									>
								</el-tooltip>
							</template>
						</el-table-column>
					</el-table>
					<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
				</div>
			</pane>
		</splitpanes>
		<DetailDialog ref="dialogRef" />
	</div>
</template>

<script setup lang="ts" name="realTimeData">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, onlineStatus } from '/@/api/platform/airMonitorEquipment';
import { fetchAreaList as fetchTreeList } from '/@/api/platform/codeArea';
import { handleTree } from '/@/utils/other';
import DetailDialog from './detail.vue';
interface Tree {
	[key: string]: any;
}
const queryRef = ref();
const dialogRef = ref();
const showSearch = ref(true);
const filterText = ref('');
const treeRef = ref();

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		tag: null,
	},
	pageList: fetchList,
	createdIsNeed: false,
});
const statistic = reactive({ offline: '0', online: '0', total: '0' });
//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	getDataList();
	getStatictis();
};
// 区域树使用的数据
const treeData = reactive({
	list: [],
	defaultProps: {
		label: 'name',
	},
});
const handleSearch = () => {
	getDataList();
	getStatictis();
};
// 搜索树监听
watch(filterText, (val) => {
	treeRef.value!.filter(val);
});
// 点击树
const handleNodeClick = (e: any) => {
	state.queryForm.tag = e.id;
	getDataList();
	getStatictis();
};
const filterNode = (value: string, data: Tree) => {
	if (!value) return true;
	return data.name.includes(value);
};
const getTreeDate = async () => {
	await fetchTreeList().then((res: any) => {
		const records = res.data;
		if (records.length) {
			treeData.list = handleTree(records, 'id', 'pid', 'children', '1111111111111111111');
			state.queryForm.tag = treeData.list[0]?.id;
			getDataList();
			getStatictis();
			nextTick(() => {
				unref(treeRef)?.setCurrentKey(state.queryForm.tag, true);
			});
		}
	});
};
onMounted(() => {
	getTreeDate();
});
const getStatictis = () => {
	onlineStatus(state.queryForm).then((res) => {
		Object.assign(statistic, res.data);
	});
};
</script>
